package controller;

import entity.Meeting;
import entity.PageEntity;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import service.MeetingService;

import javax.security.sasl.SaslServer;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@WebServlet("/meeting/*")
public class MeetingServlet extends BaseServlet{

    private static final Log log = LogFactory.getLog(MeetingServlet.class);
    private MeetingService meetingService = new MeetingService();

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    protected void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //前端提交的所有参数
        Map<String,String> param = getParam(req);

        Integer currPage = Integer.parseInt(param.get("currPage"));
        String title = param.get("title");
        String status = param.get("status");

        if("".equals(status)){
            status = "-1";
        }

        //查询分页数据
        PageEntity<Meeting>pageData = meetingService.findPage(title,status,currPage);

        req.setAttribute("pageData", pageData);
        req.setAttribute("title",title);
        req.setAttribute("status",status);

        req.getRequestDispatcher("/html/meeting.jsp").forward(req,resp);

    }

    /**
     * 发布会议
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        req.setCharacterEncoding("utf-8");
        Map<String,String> param = getParam(req);

        String title = param.get("title");
        String content = param.get("content");
        Long deptId = Long.parseLong(param.get("deptId"));
        String deptName = param.get("deptName");
        Date startTime =sdf.parse(param.get("startTime"));
        Date endTime =sdf.parse(param.get("endTime"));

        Meeting meeting = new Meeting();
        meeting.setTitle(title);
        meeting.setContent(content);
        meeting.setDeptId(deptId);
        meeting.setDeptName(deptName);
        meeting.setStartTime(startTime);
        meeting.setEndTime(endTime);
        meeting.setPublishDate(new Date());
        meeting.setStatus(0L);

        

        String makeUser = param.get("makeUser");
        if(makeUser.length()>1){
            //最起码勾选了两个用户[3,4,5]

            //3,4,5]
            makeUser = makeUser.substring(1);

            //3 , 4, 5
            makeUser = makeUser.substring(0, makeUser.length()-1);

            //3,4,5
            makeUser = makeUser.replaceAll(" ", "");

            meeting.setMakeUser(makeUser);

        }else {
            //只勾选了一个抄送用户
            meeting.setMakeUser(makeUser);
        }

        meetingService.save(meeting);

        resp.sendRedirect("/meeting/findPage?currPage=1");


    }

    /**
     * 会议详情
     * @param req
     * @param resp
     * @throws Exception
     */
    protected void meetingDetail(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        //会议id
        Long meetingId = Long.parseLong(req.getParameter("id"));

        //1.会议详情
        Meeting meeting = meetingService.findById(meetingId);

        //2.应到人数
        String[] makeUser = meeting.getMakeUser().split(",");
        Integer makeUserCount = makeUser.length;

        //3.实到人数
        Integer realCount = meetingService.countByMeetingId(meetingId);

        //4.未到人数
        Integer noCount = makeUserCount - realCount;

        //5.我是否参加了本次会议
        Boolean isJoinMeeting = meetingService.isMeetingJoin(loginUser.getId(),meetingId);

        //6.我是否能参加本次会议
        Boolean isJoin = Arrays.asList(makeUser).contains(loginUser.getId() +"");

        req.setAttribute("meeting", meeting);
        req.setAttribute("makeUserCount", makeUserCount);
        req.setAttribute("realCount", realCount);
        req.setAttribute("noCount", noCount);
        req.setAttribute("isJoinMeeting", isJoinMeeting);
        req.setAttribute("isJoin", isJoin);

        req.getRequestDispatcher("/html/meeting_detail.jsp").forward(req,resp);
    }

    /**
     *参加/取消会议
     * @param req
     * @param resp
     * @throws Exception
     */
    protected void joinMeeting(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        //会议id
        Long meetingId = Long.parseLong(req.getParameter("id"));

        Boolean isJoinMeeting = meetingService.isMeetingJoin(loginUser.getId(), meetingId);

        if(isJoinMeeting){

            //如果我参加了会议，我是来取消会议
            meetingService.unJoinMeeting(loginUser.getId(),meetingId);
        }else {
            //没参加会议，我是来参加会议的
            meetingService.joinMeeting(loginUser.getId(),meetingId);
        }

    }
}
